package mya_dc.master_server.database;

import java.util.HashSet;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import mya_dc.shared_classes.ConnectInfo;

/**
 * The slaves' DB
 * 
 * @author      Yuval Shimron
 * <br>			MYA
 */
public class SlavesHashSet extends HashSet<ConnectInfo> implements TableModel
{

	private static final long serialVersionUID = 1L;
	
	public SlavesHashSet()
	{
		super();
	}
	
	public static final String[] ColumnName = {"#","IP Address", "Accepts on Port"};
	public static final int ColumnCount = 3;

	@Override
	public void addTableModelListener(TableModelListener l) {
		/* no implementation */
	}

	private synchronized Class<?> getColumnClassSync(int columnIndex) {
		return getValueAt(0, columnIndex).getClass();
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return getColumnClassSync(columnIndex);
	}

	@Override
	public int getColumnCount() {
		return ColumnCount;
	}

	@Override
	public String getColumnName(int columnIndex) {
		return ColumnName[columnIndex];
	}

	private synchronized int getRowCountSync() {
		return size();
	}
	
	@Override
	public int getRowCount() {
		return getRowCountSync();
	}

	private synchronized Object getValueAtSync(int rowIndex, int columnIndex)
	{
		int i = 0;
		ConnectInfo thisInfo = null;
		for (ConnectInfo tempInfo : this)
		{
			if ((i++) == rowIndex)
			{
				thisInfo = tempInfo;
				break;
			}
		}
		try {
			switch (columnIndex)
			{
				case 0: return i;
				case 1: return thisInfo != null ? thisInfo.address : "n/a";
				case 2: return thisInfo != null ? thisInfo.listenPort : "n/a";
			}
		}
		catch(Exception e)
		{
			return "n/a";
		}
		return null;
	}
	
	@Override
	public Object getValueAt(int rowIndex, int columnIndex)
	{
		return getValueAtSync(rowIndex, columnIndex);
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	@Override
	public void removeTableModelListener(TableModelListener l) {
		/* no implementation */
	}

	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		/* no implementation */
	}
}
